package org.example.demo.service.userService.impl;

import org.example.demo.service.userService.ShopCartService;
import org.example.mbg.mapper.*;
import org.example.mbg.model.*;
import org.example.demo.dto.PullUserCartItems;
import org.example.mbg.mapper.ProductMapper;
import org.example.mbg.mapper.ShopCartMapper;
import org.example.mbg.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户购物车service
 */
@Service
public class ShopCartServiceImpl implements ShopCartService {
    @Autowired
    private ShopCartMapper shopCartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductByCartMapper productByCartMapper;
    @Autowired
    private UserMapper userMapper;

    /*
     * 根据用户id返回购物车列表
     */
    @Override
    public List<PullUserCartItems> listShopCart(Long user_id) {
        var x = productByCartMapper.selectProductByUserId(user_id).stream().map(e -> {
            PullUserCartItems pullUserCartItems = new PullUserCartItems();
            pullUserCartItems.setIid(e.getId().toString());
            pullUserCartItems.setImgURL(e.getPic());
            pullUserCartItems.setTitle(e.getName());
            pullUserCartItems.setDesc(e.getDescription());
            pullUserCartItems.setPrice(e.getDiscount());
            pullUserCartItems.setCount(e.getQuantity());
            pullUserCartItems.setChecked(false);
            pullUserCartItems.setProductId(e.getProductId());
            return pullUserCartItems;
        }).collect(Collectors.toList());
        return x;
    }

    /*
    根据购物车id修改数量
     */
    @Override
    public String alterCart(Long id, Long quantity) {
        ShopCart shopCart = shopCartMapper.selectByPrimaryKey(id);
        shopCart.setQuantity(quantity);
        shopCartMapper.updateByPrimaryKey(shopCart);
        return "ok";
    }

    /*
    根据用户id和商品id筛选某一件产品
     */
    @Override
    public ShopCart SelectShopCartByUserIdAndProductId(Long user_id, Long product_id) {
//        List<ProductByCart> select_list = productByCartMapper.selectProductByUserAndProductId(user_id, product_id);
//        if (select_list.size() == 0) {
//            return null;
//        }
//        return select_list.get(0);
        ShopCartExample shopCartExample = new ShopCartExample();
        shopCartExample.createCriteria().andUserIdEqualTo(user_id).andProductIdEqualTo(product_id);
        List<ShopCart> shopCarts = shopCartMapper.selectByExample(shopCartExample);
        if (shopCarts.size() > 0) {
            return shopCarts.get(0);
        }
        return null;


    }


    /*
    根据产品id和用户id添加商品
     */
    @Override
    public int addShopCart(Long user_id, Long product_id, Long count) {
        //操作数
        int operation_count = 0;
        // 判断商品是否存在，是否上架
        if (validProduct(product_id, count)) {
            //查找是否购物车中存在该商品
            ShopCart shopCart = this.SelectShopCartByUserIdAndProductId(user_id, product_id);
            if (shopCart == null) {
                //查找数据库产品信息
//                Product product = productMapper.selectByPrimaryKey(product_id);

                ShopCart newCart = new ShopCart();

                newCart.setProductId(product_id);
                newCart.setUserId(user_id);
                newCart.setQuantity(count);
                newCart.setCreateDate(new Date());

                operation_count = shopCartMapper.insertSelective(newCart);
            } else {
                count = shopCart.getQuantity() + count;
                ShopCart newCart = new ShopCart();
                newCart.setQuantity(count);
                newCart.setCreateDate(new Date());
                newCart.setId(shopCart.getId());

                operation_count = shopCartMapper.updateByPrimaryKeySelective(newCart);
            }

        }
        return operation_count;
    }

    //删除商品
    @Override
    public int deleteShopCart(Long cartId) {
        return shopCartMapper.deleteByPrimaryKey(cartId);
    }

    @Override
    public int clearAll(Long user_id) {
        ShopCartExample shopCartExample = new ShopCartExample();
        shopCartExample.createCriteria().andUserIdEqualTo(user_id);
        return shopCartMapper.deleteByExample(shopCartExample);
    }

//    @Override
//    public HashMap<String, Object> shopCartToOrder(ShopCartParam shopCartParams) {
//        //返回hash,存uid,商品列表，总金额
//        HashMap<String, Object> requestPreOrder = new HashMap<>();
//
//        //用列表存储在购物车表里查找的记录
//        List<ShopCartSubmitParam> shopCarts = new ArrayList<>();
//        List<Long> productIds = shopCartParams.getProductIds();
//
//        ShopCartExample example = new ShopCartExample();
//        System.out.println(example);
//        System.out.println(productIds);
//        example.createCriteria()
//                .andUserIdEqualTo(shopCartParams.getUserId())
//                .andProductIdIn(productIds);
//
//        BigDecimal totalMoney = BigDecimal.ZERO;
//        List<ShopCart> shopCartList = shopCartMapper.selectByExample(example);
//
//        for (ShopCart shopCart_num : shopCartList) {
//
//            BigDecimal quantity = new BigDecimal(shopCart_num.getQuantity().toString());
//            totalMoney = totalMoney.add(shopCart_num.getPrice().multiply(quantity));
//            ShopCartSubmitParam shopCartSubmitParam = new ShopCartSubmitParam();
//            BeanUtils.copyProperties(shopCart_num, shopCartSubmitParam);
//            shopCarts.add(shopCartSubmitParam);
//
//        }
//
//        requestPreOrder.put("userId", shopCartParams.getUserId());
//        requestPreOrder.put("ShopCarts", shopCarts);
//        requestPreOrder.put("totalMoney", totalMoney);
//
//        return requestPreOrder;
//    }

    private boolean validProduct(Long product_id, Long count) {
        Product product = productMapper.selectByPrimaryKey(product_id);
        // 判断商品是否存在，是否上架,就不判断库存够不够了
        if (product == null) {
            return false;
        }
        return true;

    }
}
